package com.smm.web.vmc.filter;

import com.smm.web.vmc.service.AdminService;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by zhenghao on 2016/9/6.
 * 管理员登陆验证
 */
@Slf4j
public class AdminLoginFilter implements Filter {



    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        log.info("--------------in admin login filter");

        HttpServletRequest request = (HttpServletRequest) servletRequest;

        HttpServletResponse response = (HttpServletResponse) servletResponse;

        //检查是否登陆
        if(request.getSession().getAttribute(AdminService._ADMIN_IN_SESSION)==null){

            //没有登陆，跳转到登陆页面
            response.sendRedirect(request.getContextPath()+"/adminLogin.do");

            return;

        }

        //已经登陆，继续请求跳转
        filterChain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {

    }
}
